package com.lm.nacosconfig.component;

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.util.Map;


@Component
public class ThreadPoolConfigListener {


    @Autowired
    private DynamicThreadPoolManager threadPoolManager;


    @NacosConfigListener(dataId = "dynamic-threadpool.yaml", type = ConfigType.YAML, timeout = 3000)
    public void onReceived(String configContent) {
        Yaml yaml = new Yaml();
        Map<String, Object> config = yaml.load(configContent);
        Map<String, Object> threadPoolConfig = (Map<String, Object>) config.get("threadpool");

        int corePoolSize = (int) threadPoolConfig.get("corePoolSize");
        int maxPoolSize = (int) threadPoolConfig.get("maximumPoolSize");
        int queueCapacity = (int) threadPoolConfig.get("queueCapacity");

        // 更新线程池参数
        threadPoolManager.updateThreadPool(corePoolSize, maxPoolSize, queueCapacity);
    }
}
